package com.aspire.core;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class JsonFromDB extends TestCase {
	
	private static final Logger logger = Logger.getLogger(JsonFromDB.class);
	private static String savePath = "F:\\ua.json";//生成文件保存目录
	/**
	 * Create the test case
	 * 
	 * @param testName
	 *            name of the test case
	 */
	public JsonFromDB(String testName) {
		super(testName);
	}

	/**
	 * @return the suite of tests being tested
	 */
	public static Test suite() {
		return new TestSuite(JsonFromDB.class);
	}

	/**
	 * Rigourous Test :-)
	 * 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws ConfigurationException
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 */
	public void testApp() throws ConfigurationException,
			ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {

		CreateUAServiceImpl createUAService = new CreateUAServiceImpl();
		
		logger.info(StringUtils.center("开始生成通联UA数据文件", 80, "*"));
		long starttime = System.currentTimeMillis();

		List<Map<String, Object>> resultList = null;

		resultList = createUAService.getDevice();
		logger.info("Device(" + resultList.size() + ")");
		String deviceStr = "<!--Device-->" + JSONArray.fromObject(resultList)
				+ "<!--Device-->";

		resultList.clear();
		resultList = createUAService.getFilterDeviceList();
		logger.info("FilterDevice(" + resultList.size() + ")");
		String filterDeviceStr = "<!--FilterDevice-->"
				+ JSONArray.fromObject(resultList) + "<!--FilterDevice-->";

		resultList.clear();
		resultList = createUAService.getDeviceBrand();
		logger.info("DeviceBrand(" + resultList.size() + ")");
		String deviceBrandStr = "<!--DeviceBrand-->"
				+ JSONArray.fromObject(resultList) + "<!--DeviceBrand-->";

		resultList.clear();
		resultList = createUAService.getDeviceKeyWord();
		logger.info("DeviceKeyWord(" + resultList.size() + ")");
		String deviceKeyWordStr = "<!--DeviceKeyWord-->"
				+ JSONArray.fromObject(resultList) + "<!--DeviceKeyWord-->";

		resultList.clear();
		resultList = createUAService.getDeviceOS();
		logger.info("DeviceOS(" + resultList.size() + ")");
		String deviceOSStr = "<!--DeviceOS-->"
				+ JSONArray.fromObject(resultList) + "<!--DeviceOS-->";

		resultList.clear();
		resultList = createUAService.getDeviceSeries();
		logger.info("DeviceSeries(" + resultList.size() + ")");
		String deviceSeriesStr = "<!--DeviceSeries-->"
				+ JSONArray.fromObject(resultList) + "<!--DeviceSeries-->";

		resultList.clear();
		resultList = createUAService.getDeviceGWUA();
		logger.info("DeviceGWUA(" + resultList.size() + ")");
		String deviceGWUAStr = "<!--DeviceGWUA-->"
				+ JSONArray.fromObject(resultList) + "<!--DeviceGWUA-->";

		resultList.clear();
		resultList = createUAService.getDeviceUA();
		logger.info("DeviceUA(" + resultList.size() + ")");
		String deviceUAStr = "<!--DeviceUA-->"
				+ JSONArray.fromObject(resultList) + "<!--DeviceUA-->";

		try {
			// logger.info("文件长度=" + toFileSBStr.length());
			File saveFile = new File(savePath);
			if( !saveFile.exists() )
			{
				FileUtils.writeStringToFile(saveFile, "");
			}
			
			FileOutputStream fos = new FileOutputStream(saveFile);
			IOUtils.write("<!--CreateTime-->" + System.currentTimeMillis()
					+ "<!--CreateTime-->", fos, "gbk");
			IOUtils.write(deviceStr, fos, "gbk");
			IOUtils.write(filterDeviceStr, fos, "gbk");
			IOUtils.write(deviceBrandStr, fos, "gbk");
			IOUtils.write(deviceKeyWordStr, fos, "gbk");
			IOUtils.write(deviceOSStr, fos, "gbk");
			IOUtils.write(deviceSeriesStr, fos, "gbk");
			IOUtils.write(deviceGWUAStr, fos, "gbk");
			IOUtils.write(deviceUAStr, fos, "gbk");
			IOUtils.closeQuietly(fos);

			File versionFile = new File((new File(savePath)).getParent()+File.separator+"version.txt");
			FileUtils.writeStringToFile(versionFile, "Ver:" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			// FileWriter fw = new FileWriter(new File(savePath));
			// fw.write(toFileSBStr);

		} catch (IOException e) {
			logger.error(e);
		}

		long end = System.currentTimeMillis();
		logger.info(StringUtils.center("通联UA数据文件生成完毕(" + (end - starttime)
				+ "ms!)", 80, "*"));
	}

}
